package com.esig.demo;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class login extends JFrame{
    public static void main(String[] args) {

        JFrame frame = new JFrame("登录");
        frame.setSize(300,250);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.setLayout(new GridLayout(4,1));

        JPanel panel = new JPanel();
        JLabel label1 = new JLabel("用户名:");
        JTextField username = new JTextField(20);
        panel.add(label1);
        panel.add(username);

        JPanel panel2 = new JPanel();
        JLabel lab2 = new JLabel("密码:");
        JPasswordField psw = new JPasswordField(10);
        psw.setEchoChar('*');
        panel2.add(lab2);
        panel2.add(psw);

        JPanel panel4 = new JPanel();
        JLabel lab4 = new JLabel("确认密码:");
        JPasswordField pswconfirm = new JPasswordField(10);
        pswconfirm.setEchoChar('*');
        panel4.add(lab4);
        panel4.add(pswconfirm);

        JPanel panel3 = new JPanel();
        JButton button1 = new JButton("注册");
        JButton button2 = new JButton("重置");
        panel3.add(button1);
        panel3.add(button2);

        frame.add(panel);
        frame.add(panel2);
        frame.add(panel4);
        frame.add(panel3);

        RegisterListener rl = new RegisterListener(frame, username, psw,pswconfirm);
        button1.addActionListener(rl);

        Reset rs = new Reset(frame, username, psw,pswconfirm,panel);
        button1.addActionListener(rs);

        frame.setVisible(true);
    }
}

class RegisterListener implements ActionListener {
    private javax.swing.JFrame register;
    private javax.swing.JTextField username;
    private javax.swing.JPasswordField psw;
    private javax.swing.JPasswordField pswconfirm;

    public RegisterListener(javax.swing.JFrame register, javax.swing.JTextField username, javax.swing.JPasswordField psw, javax.swing.JPasswordField pswconfirm) {
        this.register = register;
        this.username = username;
        this.psw = psw;
        this.pswconfirm = pswconfirm;

    }

    public void actionPerformed(ActionEvent e) {
        if (username.getText().toString().length() < 10) {
            JOptionPane.showMessageDialog(null, "用户名长度小于10！", "消息", 1);
        }
        if (!(psw.getText().toString().equals(pswconfirm.getText().toString()))) {
            JOptionPane.showMessageDialog(null, "密码与确认密码不一致！", "消息", 1);
        }
        if(!(username.getText().toString().length() < 10) && (psw.getText().toString().equals(pswconfirm.getText().toString()))){
            JOptionPane.showMessageDialog(null, "注册成功！", "消息", 1);
        }else {
            JOptionPane.showMessageDialog(null, "注册不成功！", "消息", 1);
        }

    }
}

class Reset implements ActionListener {
    private javax.swing.JFrame register;
    private javax.swing.JTextField username;
    private javax.swing.JPasswordField psw;
    private javax.swing.JPasswordField pswconfirm;
    private javax.swing.JPanel panel;

    public Reset(javax.swing.JFrame register, javax.swing.JTextField username, javax.swing.JPasswordField psw, javax.swing.JPasswordField pswconfirm,javax.swing.JPanel panel) {
        this.register = register;
        this.username = username;
        this.psw = psw;
        this.pswconfirm = pswconfirm;
        this.panel = panel;
    }

    public void actionPerformed(ActionEvent e) {
        username.setText("");
        psw.setText("");
        pswconfirm.setText("");
        panel.repaint();
    }
}